package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.constant.Constant;
import com.csun.cmny.provider.dao.OmcCycleExpenseItemMapper;
import com.csun.cmny.provider.dao.OmcOldLeaveMapper;
import com.csun.cmny.provider.dto.OmcOldLeaveQueryDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.old_leave.OldLeaveDaysDto;
import com.csun.cmny.provider.model.dto.old_leave.OldLeaveOldmanQueryDto;
import com.csun.cmny.provider.model.dto.revise.GroupPaymentFeeQueryDto;
import com.csun.cmny.provider.model.vo.old_leave.OldLeaveDaysVo;
import com.csun.cmny.provider.model.vo.old_leave.OldLeaveOldmanVo;
import com.csun.cmny.provider.model.vo.revise.OldmanPaymentFeeVo;
import com.csun.cmny.provider.model.vo.revise.OldmanSetMealItemVo;
import com.csun.cmny.provider.model.vo.revise.OldmanSetMealVo;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.pojo.OmcOldLeave;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.utils.CronUtil;
import com.csun.cmny.provider.utils.DateUtil;
import com.csun.cmny.provider.vo.OldLeaveVo;
import com.csun.cmny.provider.vo.OldUnLeaveVo;
import com.csun.cmny.provider.vo.OldmanBedInfoVo;
import com.csun.cmny.provider.vo.OmcAdjustRefundData;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author chenzheng
 * @description:
 * @date 2019/7/22 14:13
 */
@Slf4j
@Service
@Transactional
public class OmcOldLeaveServiceImpl extends BaseService<OmcOldLeave> implements OmcOldLeaveService {
    @Resource
    private OmcOldLeaveMapper omcOldLeaveMapper;

    @Resource
    private OmcReturnFeeConfigService returnFeeConfigService;

    @Resource
    private OmcCycleExpenseItemService cycleExpenseItemService;

    @Resource
    private OmcExpenseTypeModelCodeService expenseTypeModelCodeService;

    @Resource
    private OmcPeriodLeaveReturnFeeService periodLeaveReturnFeeService;

    @Resource
    private OmcBillSetMealItemService billSetMealItemService;

    @Resource
    private OmcPaymentCycleService paymentCycleService;
    @Resource
    private OmcOldManSupplementService omcOldManSupplementService;
    @Resource
    private OmcCycleExpenseItemMapper omcCycleExpenseItemMapper;
    @Resource
    private OmcBillMealService omcBillMealService;
    @Resource
    private OmcOldManBillService omcOldManBillService;
    @Resource
    private OmcOldLeaveSetMealItemService omcOldLeaveSetMealItemService;
    @Resource
    private OmcOldLeaveSetMealService omcOldLeaveSetMealService;
    @Resource
    private OmcOldLeaveItemService omcOldLeaveItemService;
    @Resource
    private OldmanService oldmanService;
    @Resource
    private BedService bedService;
    @Resource
    private OmcOldmanStatusService omcOldmanStatusService;

    /**
     * 新增或者更新老人请假记录
     *
     * @param omcOldLeave
     * @param loginAuthDto
     * @return
     */
    @Override
    public int saveOmcOldLeave(OmcOldLeave omcOldLeave, LoginAuthDto loginAuthDto) {
        //插入更新,新增的操作员ID和操作时间
        omcOldLeave.setUpdateInfo(loginAuthDto);

        //写入老人名字
        Oldman oldman = new Oldman();
        oldman.setOldmanId(omcOldLeave.getOldManId().intValue());
        Oldman omcOldMan = oldmanService.selectByOld(oldman);
        omcOldLeave.setOldManName(omcOldMan.getOldName());

        //写入床位编号
        OldmanBedInfoVo oldmanBedInfoVo = bedService.selectByOid(omcOldLeave.getOldManId().intValue());
        String bedNo = String.valueOf(oldmanBedInfoVo.getBedNo());
        String roomNo = String.valueOf(oldmanBedInfoVo.getRoomNo());
        omcOldLeave.setBedNo(bedNo);
        omcOldLeave.setRoomNo(roomNo);

        // 标记老人请假状态
        omcOldmanStatusService.oldmanLeaveEnd(omcOldLeave.getOldManId().intValue());

        if (omcOldLeave.isNew()) {//是否为新增记录
            return omcOldLeaveMapper.insertSelective(omcOldLeave);
        } else {
            return omcOldLeaveMapper.updateByPrimaryKeySelective(omcOldLeave);
        }
    }

    /**
     * 根据ID查询请假记录
     *
     * @param id
     * @return
     */
    @Override
    public OldLeaveVo showById(Long id) {
        return omcOldLeaveMapper.showById(id);
    }

    /**
     * 根据老人ID和老人姓名查询所有的请假记录
     *
     * @param omcOldLeaveQueryDto
     * @return
     */
    @Override
    public List<OldLeaveVo> showAll(OmcOldLeaveQueryDto omcOldLeaveQueryDto) {
        PageHelper.startPage(omcOldLeaveQueryDto.getPageNum(), omcOldLeaveQueryDto.getPageSize());
        return omcOldLeaveMapper.showAll(omcOldLeaveQueryDto);
    }

    /**
     * 根据老人姓名查询该班组老人信息
     *
     * @param omcOldLeaveQueryDto
     * @return
     */
    @Override
    public List<OldUnLeaveVo> selectOldManByName(OmcOldLeaveQueryDto omcOldLeaveQueryDto) {
        return omcOldLeaveMapper.selectOldManByName(omcOldLeaveQueryDto);
    }

    /**
     * 调房调级--查询老人上个周期账单执行时间至调房调级的时间间的未退费的请假和退费金额
     * @param omcOldLeaveQueryDto
     * @return
     */
    @Override
    public OmcAdjustRefundData showLeaveRefundFee(OmcOldLeaveQueryDto omcOldLeaveQueryDto) {
        Date lastDate = getLastDate(omcOldLeaveQueryDto.getGroupId());//上个周期执行时间
        if (null == lastDate) {
            throw new OmcBizException(ErrorCodeEnum.OMC10040001);
        }
        omcOldLeaveQueryDto.setPageNum(0);
        omcOldLeaveQueryDto.setPageSize(0);
        omcOldLeaveQueryDto.setLastDate(DateUtil.dateToString(lastDate));
        return omcOldLeaveMapper.showLeaveRefundFee(omcOldLeaveQueryDto);
    }

    /**
     * 根据机构,老人ID,时间范围,查询机构老人的请假信息
     *
     * @param paramMap
     * @return
     */
    @Override
    public List<OmcOldLeave> selectOldLeaveList(Map<String, Object> paramMap) {
        return omcOldLeaveMapper.selectOldLeaveList(paramMap);
    }

    /**
     * 根据机构,老人ID,时间范围,查询机构老人的请假退费周期数
     *
     * @param paramMap
     * @return
     */
    @Override
    public BigDecimal selectReturnDegree(Map<String, Object> paramMap) {
        BigDecimal result = BigDecimal.ZERO;
        List<OmcOldLeave> list = selectOldLeaveList(paramMap);
        if (null != list && list.size() > 0) {
            for (OmcOldLeave oldLeave : list) {
                if(null != oldLeave.getReturnDegree()){
                    result = result.add(oldLeave.getReturnDegree());
                }
            }
        }
        return result;
    }

    /**
     * 查看老人是否在请假中
     *
     * @param omcOldLeave
     * @return
     */
    @Override
    public int confirmLeave(OmcOldLeave omcOldLeave) {
        return omcOldLeaveMapper.confirmLeave(omcOldLeave);
    }

    /**
     * 计算退费天数
     *
     * @param groupId 机构ID
     * @param days    实际请假天数
     * @return
     */
    @Override
    public int getRefundDays(Long groupId, int days) {
        int resultDays = days;
        //查询是否有退费方案,没有退费方案,退费天数为实际请假天数
        OmcReturnFeeConfig dto = new OmcReturnFeeConfig();
        dto.setGroupId(groupId);
        OmcReturnFeeConfig returnFeeConfig = returnFeeConfigService.selectOne(dto);
        log.info("请假天数 ==》{}", days);
        log.info("请假退费配置 ==》{}", returnFeeConfig.toString());
        if (null != returnFeeConfig) {
            if (days >= returnFeeConfig.getReturnFeeDays()) {
                resultDays = (days - returnFeeConfig.getTakeOffDays()) >= 0 ? (days - returnFeeConfig.getTakeOffDays()) : 0;
            }
        }
        return resultDays;
    }

    /**
     * 批量更新请假信息
     *
     * @param list
     * @return
     */
    @Override
    public int batchUpdate(List<OmcOldLeave> list) {
        int n = 0;
        if (null != list && list.size() > 0) {
            for (OmcOldLeave omcOldLeave : list) {
                n = n + omcOldLeaveMapper.updateByPrimaryKeySelective(omcOldLeave);
            }
        }
        return n;
    }

    /**
     * 计算请假退费费用
     *
     * @param omcOldLeave
     * @param loginAuthDto
     *
     * @return
     */
    @Override
    public OmcOldLeave calculateRefundFee(OmcOldLeave omcOldLeave, LoginAuthDto loginAuthDto) {

        Long groupId = loginAuthDto.getGroupId();
        Long oldmanId = omcOldLeave.getOldManId();

        OmcOldManSupplement omcOldManSupplement = new OmcOldManSupplement();
        omcOldManSupplement.setOldManId(oldmanId);
        omcOldManSupplement = omcOldManSupplementService.selectOne(omcOldManSupplement);
        String inDate = omcOldManSupplement.getInDate();
        // 查询退费周期数
        OldLeaveDaysDto oldLeaveDaysDto = new OldLeaveDaysDto();

        oldLeaveDaysDto.setStartDate(com.csun.cmny.util.DateUtil.stringToDate(omcOldLeave.getLeaveDate()));
        oldLeaveDaysDto.setEndDate(com.csun.cmny.util.DateUtil.stringToDate(omcOldLeave.getRealDate()));
        oldLeaveDaysDto.setGroupId(groupId);
        oldLeaveDaysDto.setInDate(com.csun.cmny.util.DateUtil.stringToDate(inDate));

        // 查询老人请假退费周期数
        OldLeaveDaysVo oldLeaveDaysVo = returnFeeConfigService.queryOldLeaveDaysVo(oldLeaveDaysDto);
        log.info("请假退款天数计算 ==》{}", oldLeaveDaysVo.toString());

        BigDecimal dt = oldLeaveDaysVo.getDt(); // 请假退费周期数(Dt) = 请假天数 / 周期总天数
        BigDecimal ds = oldLeaveDaysVo.getDs(); // 请假开始周期数（Ds） = 请假结束周期数（De） - 请假退费周期数(Dt)
        BigDecimal de = oldLeaveDaysVo.getDe(); // 请假结束周期数（De） = 请假结束节点天数 / 周期总天数

        omcOldLeave.setRefundDays(oldLeaveDaysVo.getReturnDays());  // 退费天数
        omcOldLeave.setRealRefundDays(oldLeaveDaysVo.getRealLeaveDays()); // 实际请假天数
        omcOldLeave.setReturnDegree(dt);
        omcOldLeave.setRefundFee(BigDecimal.ZERO);

        if (dt.compareTo(BigDecimal.ZERO) == 0) {
            return omcOldLeave;
        }

        Map<Long, OmcOldLeaveItem> oldLeaveItemVoMap = Maps.newHashMap();

        // 查询老人收费项 （仅包含退费项）
        GroupPaymentFeeQueryDto groupPaymentFeeQueryDto = new GroupPaymentFeeQueryDto();
        groupPaymentFeeQueryDto.setOldmanId(oldmanId.intValue());
        groupPaymentFeeQueryDto.setIsLeaveReturn(Constant.EXPENSE_TYPE_MODEL_CODE_LEAVE);
        List<OldmanPaymentFeeVo> oldmanPaymentFeeVoList = omcCycleExpenseItemMapper
                .queryOldmanPaymentFeeByOldmanId(groupPaymentFeeQueryDto);
        if (PublicUtil.isEmpty(oldmanPaymentFeeVoList)) {
            log.warn("老人无缴费信息");
            return omcOldLeave;
        } else {
            // 退费项集合
            for (OldmanPaymentFeeVo opfv : oldmanPaymentFeeVoList) {
                // 按原价退款
                OmcOldLeaveItem olt = getOmcOldLeaveItem(opfv, loginAuthDto, dt);
                olt.setOldLeaveId(omcOldLeave.getId());
                oldLeaveItemVoMap.put(opfv.getExpenseItemId(), olt);
            }
        }

        // 查询老人套餐列表
        Long billId = omcOldManBillService.queryOldmanCheckInBillId(oldmanId.intValue());
        if (billId == null) {
            throw new OmcBizException(ErrorCodeEnum.OMC10038012);
        }

        BigDecimal setMealTotal = BigDecimal.ZERO;
        List<OmcOldLeaveSetMeal> omcOldLeaveSetMealList = Lists.newArrayList();
        List<OmcOldLeaveSetMealItem> omcOldLeaveSetMealItemList = Lists.newArrayList();
        List<OldmanSetMealVo> oldmanSetMealVoList = omcBillMealService.queryOldmanSetMealByBillId(billId);
        for (OldmanSetMealVo osmv : oldmanSetMealVoList) {
            BigDecimal total = BigDecimal.ZERO;
            OmcOldLeaveSetMeal omcOldLeaveSetMeal = getOmcOldLeaveSetMeal(osmv, loginAuthDto);
            omcOldLeaveSetMeal.setOldLeaveId(omcOldLeave.getId());
            for (OldmanSetMealItemVo osmiv : osmv.getOldmanSetMealItemVoList()) {
                OmcOldLeaveSetMealItem omcOldLeaveSetMealItem = getOmcOldLeaveSetMealItem(osmiv, loginAuthDto);
                omcOldLeaveSetMealItem.setOldLeaveId(omcOldLeave.getId());
                BigDecimal itemTotal = BigDecimal.ZERO;
                Long expenseItemId = osmiv.getExpenseItemId();
                OmcOldLeaveItem olt = oldLeaveItemVoMap.get(expenseItemId);
                if (PublicUtil.isEmpty(olt)) {
                    continue;
                }
                // 需要进行退费
                BigDecimal setMealReturnPercent = osmiv.getUsedDegree().add(osmiv.getResidueDegree());
                log.info("套餐退费数 setMealReturnPercent = {}", setMealReturnPercent);
                if (setMealReturnPercent.compareTo(BigDecimal.ZERO) > 0
                        && setMealReturnPercent.compareTo(ds) > 0) {
                    // 套餐需要进行退费
                    setMealReturnPercent = setMealReturnPercent.subtract(ds);

                    if (setMealReturnPercent.compareTo(dt) >= 0) {
                        // 全部套餐退
                        oldLeaveItemVoMap.remove(expenseItemId);
                        omcOldLeaveSetMealItem.setReturnDegree(dt);
                        itemTotal = dt.multiply(osmiv.getReturnStandard()).setScale(2, BigDecimal.ROUND_HALF_UP);
                    } else {
                        // 部分套餐退 & 部分原价退
                        omcOldLeaveSetMealItem.setReturnDegree(setMealReturnPercent);
                        itemTotal = setMealReturnPercent.multiply(osmiv.getReturnStandard()).setScale(2, BigDecimal.ROUND_HALF_UP);
                        olt.setReturnDegree(dt.subtract(setMealReturnPercent));
                    }
                }
                total = total.add(itemTotal);
                omcOldLeaveSetMealItem.setTotalFee(itemTotal);
                if (itemTotal.compareTo(BigDecimal.ZERO) > 0) {
                    omcOldLeaveSetMealItemList.add(omcOldLeaveSetMealItem);
                }
            }
            setMealTotal = setMealTotal.add(total);
            omcOldLeaveSetMeal.setTotalFee(total);
            if (total.compareTo(BigDecimal.ZERO) > 0) {
                omcOldLeaveSetMealList.add(omcOldLeaveSetMeal);
            }
        }

        // 计算退费金额
        BigDecimal itemTotal = BigDecimal.ZERO;
        List<OmcOldLeaveItem> omcOldLeaveItemList = Lists.newArrayList();
        for (OmcOldLeaveItem item : oldLeaveItemVoMap.values()) {
            BigDecimal totalFee = item.getActualFee().multiply(item.getReturnDegree()).setScale(2, BigDecimal.ROUND_HALF_UP);
            item.setTotalFee(totalFee);
            omcOldLeaveItemList.add(item);
            System.out.println("请假退费 ==> " + item);
            itemTotal = itemTotal.add(totalFee);
        }
        BigDecimal returnTotal = itemTotal.add(setMealTotal);
        omcOldLeave.setRefundFee(returnTotal);

        // 收费项退费入库
        omcOldLeaveItemService.batchSave(omcOldLeaveItemList);

        // 套餐退费项入库
        omcOldLeaveSetMealService.batchSave(omcOldLeaveSetMealList);
        omcOldLeaveSetMealItemService.batchSave(omcOldLeaveSetMealItemList);

        return omcOldLeave;
    }

    private OmcOldLeaveSetMealItem getOmcOldLeaveSetMealItem(OldmanSetMealItemVo osmiv, LoginAuthDto loginAuthDto) {

        OmcOldLeaveSetMealItem omcOldLeaveSetMealItem = new OmcOldLeaveSetMealItem();

        BeanUtils.copyProperties(osmiv, omcOldLeaveSetMealItem);
        omcOldLeaveSetMealItem.setUpdateInfo(loginAuthDto);
        omcOldLeaveSetMealItem.setItemName(osmiv.getExpenseItemName());
        omcOldLeaveSetMealItem.setItemType(osmiv.getExpenseTypeName());
        omcOldLeaveSetMealItem.setItemFee(osmiv.getChargeStandard());

        return omcOldLeaveSetMealItem;
    }

    private OmcOldLeaveSetMeal getOmcOldLeaveSetMeal(OldmanSetMealVo osmv, LoginAuthDto loginAuthDto) {

        OmcOldLeaveSetMeal omcOldLeaveSetMeal = new OmcOldLeaveSetMeal();

        BeanUtils.copyProperties(osmv, omcOldLeaveSetMeal);
        omcOldLeaveSetMeal.setUpdateInfo(loginAuthDto);

        return omcOldLeaveSetMeal;
    }

    private OmcOldLeaveItem getOmcOldLeaveItem(OldmanPaymentFeeVo opfv, LoginAuthDto loginAuthDto, BigDecimal dt) {

        OmcOldLeaveItem omcOldLeaveItem = new OmcOldLeaveItem();
        BeanUtils.copyProperties(opfv, omcOldLeaveItem);
        omcOldLeaveItem.setUpdateInfo(loginAuthDto);
        omcOldLeaveItem.setReturnDegree(dt);

        return omcOldLeaveItem;
    }

    // TODO 接口作废
    /**
     * 计算请假退费费用
     *
     * @param omcOldLeave
     * @return
     */
//    @Override
//    public OmcOldLeave calculateRefundFee(OmcOldLeave omcOldLeave, LoginAuthDto loginAuthDto) {
//        BigDecimal refoundFee = new BigDecimal(0);//请假退费费用
//        Date lastDate = getLastDate(omcOldLeave.getGroupId());//上个周期执行时间
//        if (null == lastDate) {
//            throw new OmcBizException(ErrorCodeEnum.OMC10040001);
//        }
//        //查询是否有退费方案,没有退费方案不予生成周期账单,一个机构只有一个退费方案配置
//        OmcReturnFeeConfig dto = new OmcReturnFeeConfig();
//        dto.setGroupId(omcOldLeave.getGroupId());
//        OmcReturnFeeConfig returnFeeConfig = returnFeeConfigService.selectOne(dto);
//        if (null == returnFeeConfig) {
//            throw new OmcBizException(ErrorCodeEnum.OMC10040002);
//        }
//        //查询老人已退费的退费周期数
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("groupId", omcOldLeave.getGroupId());
//        paramMap.put("lastDate", DateUtil.dateToString(lastDate));
//        paramMap.put("oldManId", omcOldLeave.getOldManId());
//        BigDecimal returnDegree = selectReturnDegree(paramMap);
//        if (null != omcOldLeave && omcOldLeave.getRealRefundDays() > 0) {
//            List<OmcPeriodLeaveReturnFee> resultList = new ArrayList<>();
//            if (null != returnFeeConfig) {//机构需有退费方案
//                //如果该条请假已有计算请假退费详情,将其删除
//                OmcPeriodLeaveReturnFee dtoP = new OmcPeriodLeaveReturnFee();
//                dtoP.setLeaveId(omcOldLeave.getId());
//                periodLeaveReturnFeeService.delete(dtoP);
//
//                //请假退费周期数 = 退费天数/周期总天数
//                BigDecimal leaveCycle = new BigDecimal(omcOldLeave.getRealRefundDays()).divide(new BigDecimal(returnFeeConfig.getCycleTotalDays()), 2, BigDecimal.ROUND_HALF_UP);
//                //查询老人的周期项目
//                OmcCycleExpenseItem cycleExpenseItemDto = new OmcCycleExpenseItem();
//                cycleExpenseItemDto.setGroupId(omcOldLeave.getGroupId());
//                cycleExpenseItemDto.setOldmanId(omcOldLeave.getOldManId());
//                List<OmcCycleExpenseItem> cycleExpenseItemList = cycleExpenseItemService.select(cycleExpenseItemDto);
//                //开始计算周期费用
//                if (null != cycleExpenseItemList && cycleExpenseItemList.size() > 0) {
//                    for (OmcCycleExpenseItem item : cycleExpenseItemList) {
//                        boolean isLeaveReturn = expenseTypeModelCodeService.isLeaveReturn(item.getExpenseItemId(), omcOldLeave.getGroupId());//判断项目是否请假退费
//                        if (isLeaveReturn) {//请假退费
//                            //填充请假退费计算的基础信息
//                            OmcPeriodLeaveReturnFee omcPeriodLeaveReturnFee = new OmcPeriodLeaveReturnFee();
//                            omcPeriodLeaveReturnFee.setLeaveId(omcOldLeave.getId());
//                            omcPeriodLeaveReturnFee.setCycleTotalDays(returnFeeConfig.getCycleTotalDays());
//                            omcPeriodLeaveReturnFee.setGroupId(omcOldLeave.getGroupId());
//                            omcPeriodLeaveReturnFee.setOldManId(omcOldLeave.getOldManId());
//                            omcPeriodLeaveReturnFee.setLeaveDate(omcOldLeave.getLeaveDate());
//                            omcPeriodLeaveReturnFee.setRealDate(omcOldLeave.getRealDate());
//                            omcPeriodLeaveReturnFee.setTakeOffDays(returnFeeConfig.getTakeOffDays());
//                            omcPeriodLeaveReturnFee.setReturnFeeDays(returnFeeConfig.getReturnFeeDays());
//                            //填充退费项目详情
//                            omcPeriodLeaveReturnFee.setExpenseItemId(item.getExpenseItemId());
//                            omcPeriodLeaveReturnFee.setItemFee(item.getItemFee());
//                            omcPeriodLeaveReturnFee.setActualFee(item.getActualFee());
//                            omcPeriodLeaveReturnFee.setExpenseTypeId(item.getExpenseTypeId());
//                            omcPeriodLeaveReturnFee.setItemName(item.getItemName());
//                            omcPeriodLeaveReturnFee.setItemType(item.getItemType());
//                            omcPeriodLeaveReturnFee.setUserRateRefief(item.getUserRateRefief());
//                            omcPeriodLeaveReturnFee.setUserTypeRateRefief(item.getUserTypeRateRefief());
//                            //计算请假退费金额
//                            BigDecimal itemRefoundFee = BigDecimal.ZERO;
//                            if (item.getResidueDegree().compareTo(BigDecimal.ZERO) == 1) {//剩余周期数大于0
//                                //查询套餐退费公式
//                                OmcBillSetMealItem setMealItem = billSetMealItemService.selectMealItem(omcOldLeave.getOldManId(), item.getExpenseItemId());
//                                if (null == setMealItem) {
//                                    omcPeriodLeaveReturnFee.setReturnStandard(setMealItem.getReturnStandard());
//                                    omcPeriodLeaveReturnFee.setIsMeal(1);
//                                    if (item.getResidueDegree().compareTo(returnDegree) == 1) {//剩余周期数大于已请假退费周期数
//                                        if (item.getResidueDegree().compareTo(refoundFee.add(leaveCycle)) == 1) {//剩余周期数大于(已请假退费周期数+这次请假退费周期数)
//                                            itemRefoundFee = itemRefoundFee.add(leaveCycle.multiply(setMealItem.getReturnStandard()));
//                                        } else {
//                                            BigDecimal lastDegree = item.getResidueDegree().subtract(refoundFee);//剩余可按套餐退费方法退费的周期数
//                                            itemRefoundFee = itemRefoundFee.add(lastDegree.multiply(setMealItem.getReturnStandard()));
//                                            //其它的请假周期数按项目原价退
//                                            itemRefoundFee = itemRefoundFee.add(leaveCycle.subtract(lastDegree).multiply(omcPeriodLeaveReturnFee.getItemFee()));
//                                        }
//                                    } else {
//                                        itemRefoundFee = itemRefoundFee.add(leaveCycle.multiply(omcPeriodLeaveReturnFee.getItemFee()));
//                                    }
//                                } else {
//                                    itemRefoundFee = itemRefoundFee.add(leaveCycle.multiply(omcPeriodLeaveReturnFee.getItemFee()));
//                                }
//
//                            } else {
//                                itemRefoundFee = itemRefoundFee.add(leaveCycle.multiply(omcPeriodLeaveReturnFee.getItemFee()));
//                            }
//                            refoundFee = refoundFee.add(itemRefoundFee);
//                            omcPeriodLeaveReturnFee.setReturnFee(itemRefoundFee.setScale(2));
//                            omcPeriodLeaveReturnFee.setReturnDegree(leaveCycle.setScale(2));
//                            omcPeriodLeaveReturnFee.setUpdateInfo(loginAuthDto);
//                            resultList.add(omcPeriodLeaveReturnFee);
//                        }
//                    }
//                }
//                //将计算请假退费细节入库
//                periodLeaveReturnFeeService.batchSave(resultList);
//                //填充退费周期数和退费费用
//                omcOldLeave.setReturnDegree(leaveCycle.setScale(2));
//                omcOldLeave.setRefundFee(refoundFee.setScale(2));
//            }
//        }
//
//        return omcOldLeave;
//    }

    /**
     * 查询可以进行请假的老人
     *
     * @param oldLeaveOldmanQueryDto the old leave oldman query dto
     *
     * @return the old leave oldman vo
     */
    @Override
    public List<OldLeaveOldmanVo> queryOldmanList(OldLeaveOldmanQueryDto oldLeaveOldmanQueryDto) {

        List<Integer> statusList = Lists.newArrayList();
        statusList.add(Constant.YIRUYUAN);
        statusList.add(Constant.SHIRUZHU);
        oldLeaveOldmanQueryDto.setOldmanStateList(statusList);

        return omcOldLeaveMapper.queryOldmanList(oldLeaveOldmanQueryDto);
    }

    /**
     * 查询请假日期在请假日期和回院日期之间的记录数量
     *
     * @param leaveDate the leave date
     *
     * @return the int
     */
    @Override
    public int queryCountByLeaveDate(Long oldmanId, String leaveDate) {

        return omcOldLeaveMapper.queryCountByLeaveDate(oldmanId, leaveDate);
    }

    /**
     * 根据日期结算请假退费费用
     *
     * @param date
     * @return
     */
    @Override
    public int closeReturnFeeByDate(Integer oldmanId, String date) {

        return omcOldLeaveMapper.closeReturnFeeByDate(oldmanId, date);
    }

    /**
     * 获取上周期执行的时间
     *
     * @param groupId
     * @return
     */
    Date getLastDate(Long groupId) {
        //查询周期设置
        OmcPaymentCycle paymentCycleDto = new OmcPaymentCycle();
        paymentCycleDto.setGroupId(groupId);
        OmcPaymentCycle paymentCycle = paymentCycleService.selectOne(paymentCycleDto);
        if (null == paymentCycle) {
            throw new OmcBizException(ErrorCodeEnum.OMC10040001);
        }
        //上次定时任务执行时间
        return CronUtil.getLastTriggerTime(paymentCycle.getPaymentRecordGenerateConfig());

    }

    /**
     * 查询今天请假老人的请假详情
     * @param groupId
     * @return
     */
    @Override
    public List<OldLeaveOldmanVo> selectLeaveOldMan(Long groupId){
        return omcOldLeaveMapper.selectLeaveOldMan(groupId);
    }
}
